package com.schedule.controller;




import com.schedule.Util.PagedGridResult;
import com.schedule.pojo.*;

import com.schedule.service.IAdminRoleService;
import com.schedule.service.IAdminService;
import com.schedule.service.IJudgeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author star
 * @since 2022-03-08
 */
@RestController
//@RequestMapping("/judge")
public class JudgeController {
    @Autowired
    private IJudgeService judgeService;

    @Autowired
    private IAdminService adminService;

    @Autowired
    private IAdminRoleService adminRoleService;

    @ApiOperation(value = "查询直接裁判")
    @GetMapping("/authority/judge/findJudgeAll")
    @ResponseBody
    public RespBean findJudge(){
        List<Judge> judges = judgeService.findAll();
        if (judges != null){
            return RespBean.success("查询裁判成功",judges);
        }
        return RespBean.error("查询裁判失败");
    }

    @ApiOperation(value = "分页查询裁判")
    @GetMapping("/authority/judge/queryJudge")
    @ResponseBody
    public RespBean getJudge(@RequestParam(name="page") Integer page,
                             @RequestParam(name="pageSize") Integer pageSize){
        PagedGridResult gridResult = judgeService.findJudge(page,pageSize);
        if (gridResult != null){
            return RespBean.success("分页查询裁判成功", gridResult);
        }
        return RespBean.error("分页查询裁判失败");
    }

    @ApiOperation(value = "设置裁判")
    @PostMapping("/authority/judge/setJudge")
    @ResponseBody
    public RespBean setJudge(@RequestParam("Name")String Name,
                             @RequestParam("other")String Other,
                             @RequestParam("userName")String userName,
                             @RequestParam("password")String password,
                             @RequestParam("phoneNum")String phoneNum){
        Admin admin = adminService.getAdminByUserName(userName);
        if(null!=admin){
            return RespBean.error("用户名已存在！");
        }else {
            AdminRegisterParam adminRegisterParam=new AdminRegisterParam();
            adminRegisterParam.setUserName(userName);
            adminRegisterParam.setPassword(password);
            adminRegisterParam.setPhoneNum(phoneNum);
            boolean result = adminService.add(adminRegisterParam);
            Admin admin2 = adminService.getAdminByUserName(userName);
            Integer Aid = admin2.getId();
            if (result == false){
                return RespBean.error("添加用户失败");
            }
            if (judgeService.setJudge(Name, Other,Aid)){
                AdminRole adminRole=new AdminRole();
                adminRole.setAdminId(Aid);
                adminRole.setRid(2);
                adminRoleService.save(adminRole);
                return RespBean.success("设置裁判成功");
            }
        }
        return RespBean.error("设置裁判失败");
    }

    @ApiOperation(value = "删除裁判")
    @DeleteMapping("/authority/judge/deleteJudge")
    @ResponseBody
    public RespBean delJudge(@RequestParam("Jid") Integer Jid){
        Judge judge = judgeService.findJudgeByJid(Jid);
        if (judge == null){
            return RespBean.error("没有该裁判");
        }else {
            Integer aid = judge.getAid();
            if (judgeService.delJudge(Jid)!=0){
                Integer integer = adminRoleService.deleteRecord(aid, 2);
                if (integer != 0){
                    Integer integer1 = adminService.deleteById(aid);
                    if (integer1 == 0){
                        return RespBean.error("删除用户失败");
                    }
                }else {
                    return RespBean.error("删除用户权限表失败");
                }
                return RespBean.success("删除裁判成功");
            }

        }
        return RespBean.error("删除裁判失败");
    }

    /**
     * 修改赛事中的裁判
     */
    @ApiOperation(value = "修改裁判")
    @PutMapping("/authority/judge/updateJudge")
    @ResponseBody
    public RespBean updateJudge(@RequestParam("Jid")Integer Jid,
                                @RequestParam("CName")String CName){
        if(judgeService.updateJudge(Jid, CName)){
            return RespBean.success("更新裁判成功");
        }
        return RespBean.error("更新裁判失败");
    }

}
